package org.luxor.commons.rabbitmq.service.publishsubscribe;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 2 号消费者
 *
 * @author Mr.Yan  @date 2019/10/16$
 */
@Component
public class TwoSubscribe {

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "exchange.fanout",type = "fanout"),
            value = @Queue("queue.twoSubscribe")
    ))
    public void onMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag)  throws IOException {
        System.out.println("2 号消费者, 接收到消息：" + message);
        // 确认消息
        channel.basicAck(deliveryTag, false);
    }

//    @RabbitListener(bindings = @QueueBinding(
//            exchange = @Exchange(value = "exchange.fanout",type = "fanout"),
//            value = @Queue("queue.twoSubscribe")
//    ))
//    public void onMessageBackup(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag)  throws IOException {
//        System.out.println("2 号消费者的代理人, 接收到消息：" + message);
//        // 确认消息
//        channel.basicAck(deliveryTag, false);
//    }
}
